﻿/**	
 * ...
 * @author Ellis
 */

package getengine.core.math;

class Vector2d
{
	
	public var x: Float;
	public var y: Float;
	
	public function new(?nx: Float, ?ny: Float) 
	{
		this.x = nx;
		this.y = ny;
	}
	
	public inline function mag(): Float
	{
		return this.x*this.x + this.y*this.y;
	}
	
	public inline function length(): Float
	{
		return cast Math.sqrt(this.mag());
	}
	
	public inline function dot(v: Vector2d): Float
	{
		return cast (this.x * v.x + this.y * v.y);
	}
	
	public inline function add(v: Vector2d): Vector2d
	{
		
		this.x = cast this.x + v.x;
		this.y = cast this.y + v.y;
		
		return this;
	}
	
	public inline function addVal(v: Float): Vector2d
	{
		this.x = cast this.x + v;
		this.y = cast this.y + v;
		
		return this;
	}
	
	public inline function sub(v: Vector2d): Vector2d
	{
		this.x = cast this.x - v.x;
		this.y = cast this.y - v.y;
		
		return this;
	}
	
	public inline function subVal(v: Float): Vector2d
	{
		this.x = this.x - v;
		this.y = this.y - v;
		
		return this;
	}
	
	public inline function mul(v: Vector2d): Vector2d
	{
		this.x = cast this.x * v.x;
		this.y = cast this.y * v.y;
		
		return this;
	}
	
	public inline function mulVal(v: Float): Vector2d
	{
		this.x = this.x * v;
		this.y = this.y * v;
		
		return this;
	}
	
	public inline function div(v: Vector2d): Vector2d
	{
		this.x = cast this.x / v.x;
		this.y = cast this.y / v.y;
		
		return this;
	}
	
	public inline function divVal(v: Float): Vector2d
	{
		this.x = cast this.x / v;
		this.x = cast this.y / v;
		
		return this;
	}
	
	public inline function normalize():Vector2d
	{
		var l : Float = this.x * this.x + this.y * this.y;
		l = 1.0 / Math.sqrt(l);
		this.x = cast this.x * l;
		this.y = cast this.y * l;
		
		return this;
	}
	
	public inline function transform(m:Matrix3): Vector2d
	{
		var nx = m._11 * this.x + m._21 * this.y + m._31;
		var ny = m._12 * this.x + m._22 * this.y + m._32;
		x = nx;
		y = ny;
		
		return this;
	}
	
	public function toString(): String
	{
		return "Vector2d(" + this.x + ", " + this.y + ")";
	}
}